package com.wc.算法提高课.E第五章_数学知识.同余.曹冲养猪;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/9 17:10
 * @description https://www.acwing.com/problem/content/description/1300/
 */
public class Main {
    /**
     * 思路：<p>
     * 中国剩余定理<p>
     * x % a[i] = b[i]<p>
     * M = /mul_1_n a[i]<p>
     * m[i] = M / a[i]<p>
     * t[i] * m[i] % a[i] == 1, t[i] 为 m[i] 的逆元<p>
     * t[i] * m[i] + a[i] * y == 1 用扩展欧几里得 <p>
     * res = /sum_1_n b[i] * m[i] * t[i] % M<p>
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 15;
    static int[] A = new int[N], B = new int[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        long M = 1;
        for (int i = 1; i <= n; i++) {
            A[i] = sc.nextInt();
            B[i] = sc.nextInt();
            M *= A[i];
        }
        long res = 0;
        for (int i = 1; i <= n; i++) {
            long Mi = M / A[i];
            long[] t = new long[1], y = new long[1];
            exgcd(Mi, A[i], t, y);
            t[0] = (t[0] % M + M) % M;
            res = (res + qmul(qmul(B[i], t[0], M), Mi, M) + M) % M;
        }
        out.println((res % M + M) % M);
        out.flush();
    }

    static long exgcd(long a, long b, long[] x, long[] y) {
        if (b == 0) {
            x[0] = 1;
            y[0] = 0;
            return a;
        }
        long d = exgcd(b, a % b, y, x);
        y[0] -= (a / b) * x[0];
        return d;
    }

    static long qmul(long a, long b, long p) {
        long res = 0;
        while (b > 0) {
            if ((b & 1) == 1) res = (res + a) % p;
            a = (a + a) % p;
            b >>= 1;
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
